package pso.particle;

public class ParticleNeighborhood {
	private Particle[] neighbours;
	private double[] bestPosition;
	private double bestValue = Double.MIN_VALUE;

	public ParticleNeighborhood(Particle[] neighbours) {
		this.neighbours = neighbours;
		for (int i = 0; i < neighbours.length; i++) {
			neighbours[i].setNeighborhood(this);
		}
	}

	public Particle[] getNeighbours() {
		return neighbours;
	}

	public void setNeighbours(Particle[] neighbours) {
		this.neighbours = neighbours;
	}

	public double[] getBestPosition() {
		if (bestPosition == null) {
			bestPosition = Helper.getBest(neighbours).getPosition().clone();
		}
		return bestPosition;
	}

	public void setBestPosition(double[] bestPosition) {
		this.bestPosition = bestPosition;
	}

	public double getBestValue() {
		return bestValue;
	}

	public void setBestValue(double bestValue) {
		this.bestValue = bestValue;
	}

	public static class Helper {
		public static Particle getBest(Particle[] neighbours) {
			Particle best = neighbours[0];
			for (int i = 1; i < neighbours.length; i++) {
				double fitness = neighbours[i].getFitness();
				if (fitness > best.getFitness()) {
					best = neighbours[i];
				}
			}
			return best;
		}
	}

}
